package client.ui.view.dialogview;

import client.Main;
import client.UserState;
import client.assets.Icons;
import client.control.Controller;
import client.control.ResponseEntity;
import client.entity.User;
import client.ui.component.button.MaterialButton;
import client.ui.component.edittext.PasswordEdit;
import client.ui.component.edittext.TextEdit;
import client.ui.view.MainPage;
import client.ui.view.base.FrameBase;
import client.ui.view.base.PanelControl;
import client.ui.view.panel.main.CartPanel;
import client.ui.view.panel.main.MyDataPanel;
import com.alibaba.fastjson.JSONObject;
import util.MD5Util;

import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.Map;

/**
  * @Description 登录对话框
  * @author 孔明浩
  * @Date 2020/3/21
  */
public class LoginDialog {
    /**
     * @Description
     * @param owner 对话框所属
     * @param parentContainer 对话框父组件
     * @author 孔明浩
     * @Date 2020/3/31
     */
    public static void showLoginDialog(Frame owner, Container parentContainer){
        JButton loginButton = MaterialButton.BlueButton("登录",null);
        JButton registerButton = MaterialButton.BlueButton("注册", null);
        JDialog dialog = new JDialog(owner,true);
        dialog.setSize(350,200);
        dialog.setResizable(false);
        dialog.setLocationRelativeTo(parentContainer);
        dialog.setTitle("登录账户");
        dialog.setIconImage(Icons.login.getImage());

        TextEdit usernameEdit = new TextEdit("请输入用户名");
        PasswordEdit passwordEdit = new PasswordEdit("请输入密码");
        JCheckBox remember = new JCheckBox("自动登录");
        remember.setFocusPainted(false);
        remember.setSelected(true);
        loginButton.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                //判断输入是否合法
                if (usernameEdit.getText().length() == 0){
                    usernameEdit.setErrorText("用户名不能为空");
                    passwordEdit.setErrorText("");
                } else if (passwordEdit.getPassword().length == 0){
                    passwordEdit.setErrorText("密码不能为空");
                    usernameEdit.setErrorText("");
                } else {
                    //输入合法取消出错信息
                    usernameEdit.setErrorText("");
                    passwordEdit.setErrorText("");
                    Thread thread = new Thread(new Runnable() {
                        @Override
                        public void run() {
                            ResponseEntity responseEntity = Controller.USER_CONTROL.login(
                                    usernameEdit.getText(), MD5Util.encodeByMd5(passwordEdit.getText()));
                            //登陆成功判断
                            if (responseEntity.getCode() == 0){
                                UserState.setLogin(true);
                                UserState.setUsername(usernameEdit.getText());
                                UserState.setToken(responseEntity.getData());
                                responseEntity = Controller.USER_CONTROL.getUserInfo(UserState.getToken());
                                if (responseEntity.getCode() == 0){
                                    User user = JSONObject.parseObject(responseEntity.getData(), User.class);
                                    UserState.setId(user.getUserId());
                                    if (remember.isSelected()){
                                        UserState.setRemember(true);
                                        UserState.update(true,UserState.getToken(),usernameEdit.getText(),null, user.getUserId());
                                    }
                                }
                                UserState.setPassword(null);
                                //重新启动主页面
                                SwingUtilities.invokeLater(new Runnable() {
                                    @Override
                                    public void run() {
                                        //重新启动面板
                                        for (Frame f:FrameBase.frameBaseList
                                             ) {
                                            f.setVisible(false);
                                            f.dispose();
                                        }
                                        FrameBase.frameBaseList.clear();
                                        PanelControl.panels.clear();
                                        Main.main(null);
                                        dialog.dispose();
                                    }
                                });
                            } else if (responseEntity.getCode() == -2){
                                passwordEdit.setErrorText("用户名或密码错误");
                            } else {
                                passwordEdit.setErrorText("认证失败");
                            }
                        }
                    });
                    thread.start();
                }
            }
        });

        registerButton.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                SwingUtilities.invokeLater(new Runnable() {
                    @Override
                    public void run() {
                        RegisterDialog.showRegisterDialog(owner,parentContainer);
                    }
                });

            }
        });

        Box vBox = Box.createVerticalBox();
        Box hBox = Box.createHorizontalBox();
        hBox.add(registerButton);
        hBox.add(remember);
        hBox.add(loginButton);
        vBox.add(usernameEdit);
        vBox.add(passwordEdit);
        vBox.add(hBox);

        JPanel panel = new JPanel(new FlowLayout(FlowLayout.CENTER));
        panel.add(vBox);
        dialog.setContentPane(panel);
        dialog.setDefaultCloseOperation(JDialog.DISPOSE_ON_CLOSE);
        dialog.setVisible(true);
    }
}
